package com.lm.controller;

import com.lm.model.DpAreaLimit;
import com.lm.service.DpAreaLimitService;
import com.meirengu.common.StatusCode;
import com.meirengu.controller.BaseController;
import com.meirengu.model.Page;
import com.meirengu.model.Result;
import com.meirengu.utils.NumberUtil;
import org.apache.commons.collections.map.HashedMap;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

@Controller
@RequestMapping("dpareaLimit")
public class DpareaLimitController extends BaseController {

    private static final Logger logger = LoggerFactory.getLogger(DpareaLimitController.class);

    @Autowired
    private DpAreaLimitService dpAreaLimitService;

    @RequestMapping(value = "list", method = RequestMethod.GET)
    @ResponseBody
    public Result listMultiple(@RequestParam(value = "page_num", required = false, defaultValue = "1") Integer pageNum,
                               @RequestParam(value = "page_size", required = false, defaultValue = "10") Integer pageSize,
                               @RequestParam(value = "sortby", required = false) String sortBy,
                               @RequestParam(value = "order", required = false) String order,
                               @RequestParam(value = "channel_no", required = false) String channelNo,
                               @RequestParam(value = "channel_name", required = false) String channelName,
                               @RequestParam(value = "area_code", required = false) String areaCode,
                               @RequestParam(value = "area_name", required = false) String areaName) {

        try {
            Map<String, Object> paramMap = new HashedMap();
            Page page = super.setPageParams(pageNum, pageSize);
            if (StringUtils.isNotBlank(sortBy)) {
                paramMap.put("sortBy", sortBy);
            }
            if (StringUtils.isNotBlank(order)) {
                paramMap.put("order", order);
            }
            if (StringUtils.isNotBlank(channelNo)) {
                paramMap.put("channelNo", channelNo);
            }
            if (StringUtils.isNotBlank(channelName)) {
                paramMap.put("channelName", channelName);
            }
            if (StringUtils.isNotBlank(areaCode)) {
                paramMap.put("areaCode", areaCode);
            }
            if (StringUtils.isNotBlank(areaName)) {
                paramMap.put("areaName", areaName);
            }

            page = dpAreaLimitService.getListByPage(page, paramMap);
            return super.setResult(StatusCode.OK, page, StatusCode.codeMsgMap.get(StatusCode.OK));
        } catch (Exception e) {
            logger.error("throw exception: {}", e);
            e.printStackTrace();
            return setResult(StatusCode.INTERNAL_SERVER_ERROR, null, StatusCode.codeMsgMap.get(StatusCode.INTERNAL_SERVER_ERROR));
        }


    }

    @RequestMapping(value = "insert", method = RequestMethod.POST)
    @ResponseBody
    public Result insert(@RequestParam(value = "create_date", required = false) String createDate,
                         @RequestParam(value = "channel_no", required = false) String channelNo,
                         @RequestParam(value = "channel_name", required = false) String channelName,
                         @RequestParam(value = "area_code", required = false) String areaCode,
                         @RequestParam(value = "area_name", required = false) String areaName,
                         @RequestParam(value = "area_limit", required = false) String areaLimit,
                         @RequestParam(value = "num", required = false) Integer num,
                         @RequestParam(value = "create_name", required = false) String createName,
                         @RequestParam(value = "create_by", required = false) String createBy) {

        DpAreaLimit dpAreaLimit = new DpAreaLimit();
        try {

            if (StringUtils.isNotBlank(createDate)) {
                SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                Date date = null;

                date = dateFormat.parse(createDate);

                dpAreaLimit.setCreateDate(date);
            }
            if (StringUtils.isNotBlank(areaLimit)) {
                dpAreaLimit.setAreaLimit(areaLimit);
            }
            if (StringUtils.isNotBlank(channelNo)) {
                dpAreaLimit.setChannelNo(channelNo);
            }
            if (StringUtils.isNotBlank(channelName)) {
                dpAreaLimit.setChannelName(channelName);
            }
            if (StringUtils.isNotBlank(areaCode)) {
                dpAreaLimit.setAreaCode(areaCode);
            }
            if (StringUtils.isNotBlank(areaName)) {
                dpAreaLimit.setAreaName(areaName);
            }
            if (num != null) {
                dpAreaLimit.setNum(num);
            }
            if (createName != null) {
                dpAreaLimit.setCreateName(createName);
            }
            if (createBy != null) {
                dpAreaLimit.setCreateBy(createBy);
            }
            int insertNum = dpAreaLimitService.insert(dpAreaLimit);
            if (insertNum < 1) {
                return setResult(StatusCode.INTERNAL_SERVER_ERROR, null, StatusCode.codeMsgMap.get(StatusCode.INTERNAL_SERVER_ERROR));
            }
            return super.setResult(StatusCode.OK, null, StatusCode.codeMsgMap.get(StatusCode.OK));
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("throw exception: {}", e);
            e.printStackTrace();
            return setResult(StatusCode.INTERNAL_SERVER_ERROR, null, StatusCode.codeMsgMap.get(StatusCode.INTERNAL_SERVER_ERROR));
        }
    }

    @RequestMapping(value = "delete", method = RequestMethod.DELETE)
    @ResponseBody
    public Result delete(@RequestParam(value = "id", required = false) Integer id) {
        try {
            if (!NumberUtil.isNullOrZero(id)) {
                int deleteNum = dpAreaLimitService.delete(id);
                if (deleteNum > 1) {
                    return super.setResult(StatusCode.OK, null, StatusCode.codeMsgMap.get(StatusCode.OK));
                }
            }
            return setResult(StatusCode.INTERNAL_SERVER_ERROR, null, StatusCode.codeMsgMap.get(StatusCode.INTERNAL_SERVER_ERROR));
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("throw exception: {}", e);
            return setResult(StatusCode.INTERNAL_SERVER_ERROR, null, StatusCode.codeMsgMap.get(StatusCode.INTERNAL_SERVER_ERROR));
        }
    }

    @RequestMapping(value = "update", method = RequestMethod.POST)
    @ResponseBody
    public Result update(@RequestParam(value = "id", required = false) Integer id,
                         @RequestParam(value = "channel_no", required = false) String channelNo,
                         @RequestParam(value = "channel_name", required = false) String channelName,
                         @RequestParam(value = "area_code", required = false) String areaCode,
                         @RequestParam(value = "area_name", required = false) String areaName,
                         @RequestParam(value = "area_limit", required = false) String areaLimit,
                         @RequestParam(value = "num", required = false) Integer num) {

        DpAreaLimit dpAreaLimit = new DpAreaLimit();
        try {
            if (!NumberUtil.isNullOrZero(id)) {
                dpAreaLimit.setId(id);
            }

            if (StringUtils.isNotBlank(areaLimit)) {
                dpAreaLimit.setAreaLimit(areaLimit);
            }
            if (StringUtils.isNotBlank(channelNo)) {
                dpAreaLimit.setChannelNo(channelNo);
            }
            if (StringUtils.isNotBlank(channelName)) {
                dpAreaLimit.setChannelName(channelName);
            }
            if (StringUtils.isNotBlank(areaCode)) {
                dpAreaLimit.setAreaCode(areaCode);
            }
            if (StringUtils.isNotBlank(areaName)) {
                dpAreaLimit.setAreaName(areaName);
            }
            if (num != null) {
                dpAreaLimit.setNum(num);
            }
            int updateNum = dpAreaLimitService.update(dpAreaLimit);
            if (updateNum < 1) {
                return setResult(StatusCode.INTERNAL_SERVER_ERROR, null, StatusCode.codeMsgMap.get(StatusCode.INTERNAL_SERVER_ERROR));
            }
            return super.setResult(StatusCode.OK, null, StatusCode.codeMsgMap.get(StatusCode.OK));
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("throw exception: {}", e);
            e.printStackTrace();
            return setResult(StatusCode.INTERNAL_SERVER_ERROR, null, StatusCode.codeMsgMap.get(StatusCode.INTERNAL_SERVER_ERROR));
        }
    }


}
